package window;



import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import org.jfree.chart.ChartPanel;

import bean.JavaBean;
import dao.TeacherDao;
import entity.PieChartExample;


public class Statisticspage {

	public JFrame statisticspageFrame;
	String id="";
	String str1="";
	String str2="";
	String[] s = { "年龄", "职称", "所在系","教授课程","联系地址" };
	String[] t = {"age","title","department","course","address"};
	/**
	 * Launch the application.
	 */

	/**
	 * Create the application.
	 */
	public Statisticspage(String id,String str1,String str2) {
		this.id=id;
		this.str1=str1;
		this.str2=str2;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		statisticspageFrame = new JFrame();
		statisticspageFrame.setBounds(-7, 0, 1380, 736);
		ImageIcon icon=new ImageIcon("D:\\eclipse\\workspace\\TeacherInformationManagementSystem\\src\\window\\1.png");
		statisticspageFrame.setIconImage(icon.getImage());
		statisticspageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		statisticspageFrame.getContentPane().setLayout(null);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 1364, 23);
		statisticspageFrame.getContentPane().add(menuBar);
		
		JMenu mnNewMenu = new JMenu("操作(ALT+1)");
		mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
		mnNewMenu.setFont(new Font("幼圆", Font.PLAIN, 15));
		mnNewMenu.setForeground(Color.DARK_GRAY);
		mnNewMenu.setMnemonic('1');
		menuBar.add(mnNewMenu);
		
		JMenuItem mntme = new JMenuItem("退出登录(E)");
		mntme.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Login lg=new Login();
				lg.LoginFrame.setVisible(true);
				statisticspageFrame.dispose();
			}
		});
		
		JMenuItem mntmNewMenuItem = new JMenuItem("返回(F)");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				IStatisticspage isp=new IStatisticspage(id);
				isp.iStatisticspageFrame.setVisible(true);
				statisticspageFrame.dispose();
			}
		});
		mntmNewMenuItem.setFont(new Font("幼圆", Font.PLAIN, 13));
		mntmNewMenuItem.setForeground(Color.DARK_GRAY);
		mntmNewMenuItem.setMnemonic('F');
		mnNewMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmg = new JMenuItem("回到首页(G)");
		mntmg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeacherHomepage th=new TeacherHomepage(id);
				th.teacherHomepageFrame.setVisible(true);
				statisticspageFrame.dispose();
			}
		});
		mntmg.setFont(new Font("幼圆", Font.PLAIN, 13));
		mntmg.setForeground(Color.DARK_GRAY);
		mntmg.setMnemonic('G');
		mnNewMenu.add(mntmg);
		mntme.setMnemonic('E');
		mntme.setForeground(Color.DARK_GRAY);
		mntme.setFont(new Font("幼圆", Font.PLAIN, 13));
		mnNewMenu.add(mntme);
		
		JLabel lblNewLabel_1 = new JLabel("欢迎你，亲爱的");
		lblNewLabel_1.setForeground(Color.DARK_GRAY);
		lblNewLabel_1.setFont(new Font("幼圆", Font.PLAIN, 12));
		lblNewLabel_1.setBounds(10, 37, 90, 15);
		statisticspageFrame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setText(id);
		lblNewLabel_2.setForeground(Color.DARK_GRAY);
		lblNewLabel_2.setFont(new Font("幼圆", Font.BOLD, 13));
		lblNewLabel_2.setBounds(98, 37, 75, 15);
		statisticspageFrame.getContentPane().add(lblNewLabel_2);
		
		MyTable table = new MyTable();
		table.setForeground(Color.DARK_GRAY);
		table.setFont(new Font("幼圆", Font.PLAIN, 13));
		 
		String[] tableHeads = new String[]{"工号","姓名","年龄","职称","所在系","教授课程","联系电话","联系地址"};
		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		dtm.setColumnIdentifiers(tableHeads);
		
		/*System.out.println(str1);
		System.out.println(str2);*/
		
		 List<JavaBean> L=TeacherDao.readList();
		List<JavaBean> l=TeacherDao.readOneList3(str1, str2);
		
		 for(int i=0;i<l.size();i++) 
		  { 
		      Object[]rowData={l.get(i).getId(),l.get(i).getName(),
		      l.get(i).getAge(),l.get(i).getTitle(),
		      l.get(i).getDepartment(),l.get(i).getCourse(),
		      l.get(i).getPhone(),l.get(i).getAddress(),
		      };
		      dtm.addRow(rowData);
		} 
		  
		  table.getColumnModel().getColumn(7).setPreferredWidth(370);
		  DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		  r.setHorizontalAlignment(JLabel.CENTER);
		  table.setDefaultRenderer(Object.class, r);
		  
		  table.addMouseListener(new MouseAdapter() {
			  	@Override
			  	public void mousePressed(MouseEvent arg0) {
			  		Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
			  		Transferable tf=new StringSelection(String.valueOf(dtm.getValueAt(table.getSelectedRow(), table.getSelectedColumn())));
			  		cb.setContents(tf, null);
			  	}
			  });
		  
		  JScrollPane JSP= new JScrollPane(table);
		  JSP.setViewportBorder(new LineBorder(Color.GRAY));
		  JSP.setBounds(10, 103, 1344, 322);
		  statisticspageFrame.getContentPane().add(JSP);
		
		JLabel label_8 = new JLabel("教师信息统计结果");
		label_8.setHorizontalAlignment(SwingConstants.CENTER);
		label_8.setForeground(Color.DARK_GRAY);
		label_8.setFont(new Font("宋体", Font.BOLD, 30));
		label_8.setBounds(0, 50, 1364, 31);
		statisticspageFrame.getContentPane().add(label_8);
		
		JLabel label = new JLabel("统计结果：共");
		label.setForeground(Color.DARK_GRAY);
		label.setFont(new Font("幼圆", Font.BOLD, 18));
		label.setBounds(957, 633, 128, 23);
		statisticspageFrame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setText(String.valueOf(l.size()));
		label_1.setForeground(Color.DARK_GRAY);
		label_1.setFont(new Font("幼圆", Font.BOLD | Font.ITALIC, 18));
		label_1.setBounds(1095, 633, 67, 23);
		statisticspageFrame.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("条");
		label_2.setForeground(Color.DARK_GRAY);
		label_2.setFont(new Font("幼圆", Font.BOLD, 18));
		label_2.setBounds(1172, 633, 100, 23);
		statisticspageFrame.getContentPane().add(label_2);
		
		JLabel label_3 = new JLabel("，以下是根据");
		label_3.setForeground(Color.DARK_GRAY);
		label_3.setFont(new Font("幼圆", Font.PLAIN, 12));
		label_3.setBounds(170, 37, 83, 15);
		statisticspageFrame.getContentPane().add(label_3);
		
		JLabel label_4 = new JLabel("的统计结果！");
		label_4.setForeground(Color.DARK_GRAY);
		label_4.setFont(new Font("幼圆", Font.PLAIN, 12));
		label_4.setBounds(332, 37, 90, 15);
		statisticspageFrame.getContentPane().add(label_4);
		
		JLabel label_5 = new JLabel("");
		for(int i=0;i<s.length;i++) {
			if(str1.equals(t[i]))
				label_5.setText(s[i]);
		}
		
		label_5.setHorizontalAlignment(SwingConstants.CENTER);
		label_5.setForeground(Color.DARK_GRAY);
		label_5.setFont(new Font("幼圆", Font.BOLD, 13));
		label_5.setBounds(247, 37, 83, 15);
		statisticspageFrame.getContentPane().add(label_5);
		
		/*String i,j;
		DecimalFormat df=new DecimalFormat("#.##");
		i=df.format((float)l.size());
		j=df.format(((float)L.size()-l.size()));*/
		
		int i,j;
		i=l.size();
		j=L.size()-l.size();
		
		/*System.out.println( Double.valueOf(i)*100 );*/
		ChartPanel chartPanel;
		try {
			chartPanel = new ChartPanel(PieChartExample.CreatePieChart(label_5.getText()+"："+str2, i , "其余", j ), false);
			chartPanel.setBounds(65, 435, 700, 252);
			statisticspageFrame.getContentPane().add(chartPanel);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
		statisticspageFrame.setTitle("教师信息管理系统");
	
	}
}
